package com.ruoyi.consumer.controller;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import static com.ruoyi.common.utils.IpAddressUtils.getAddressInfo;


@Api(tags = "文件管理")
@RestController
@RequestMapping("/api/file")
public class FileApiController {

    @Autowired
    private ServerConfig serverConfig;

    private static final String FILE_DELIMETER = ",";

    @Value("${ruoyi:/home/ruoyi/uploadPath}")
    private String lujin;


    /**
     * 前端图片上传地址（单个）
     */
    @PostMapping("/upload")
    @ApiOperation("文件单个上传")
    public AjaxResult uploadFile(MultipartFile file) {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = getAddressInfo() + fileName;
            System.out.println("url:" + url);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("url", url);
            ajaxResult.put("fileName", fileName);
            ajaxResult.put("newFileName", FileUtils.getName(fileName));
            ajaxResult.put("originalFilename", file.getOriginalFilename());
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    private static String extractFilePath(String fullPath) {
        // 找到最后一个斜杠的位置
        int lastSlashIndex = fullPath.lastIndexOf("/");

        // 截取从开头到最后一个斜杠之后的部分
        String extractedPath = fullPath.substring(lastSlashIndex + 1);

        return extractedPath;
    }

    //获取当前年月日并格式化为2023/11/15
    private static String getYearMonthDay() {
        String yearMonthDay = DateUtils.dateTimeNow("yyyy/MM/dd");
        return yearMonthDay;
    }
    @ApiOperation("文件下载")
    @GetMapping("/downloadTest")
    public void fileDownloadTest(String fileName,HttpServletResponse response)
    {
        try
        {

            String filePath = RuoYiConfig.getProfile() + getYearMonthDay() + "/" +extractFilePath(fileName);
            System.out.println("filePath: " + filePath);
            System.out.println("filePath: " + filePath);
            FileUtils.writeBytes(filePath, response.getOutputStream());

        }
        catch (Exception e)
        {
            System.out.println("下载文件失败"+e);
            System.out.println("filePath: " + RuoYiConfig.getProfile() + extractFilePath(fileName));
        }
    }


}